<?php

/**
 * role actions.
 *
 * @package    maximous
 * @subpackage role
 * @author     Mina.Gerges.Wadea
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class roleActions extends sfActions
{

    public function executeTests()
    {
        $d= new MaxUserRole(array('role_id'=>1));
        exit;
    }

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex()
    {
        //get all rols
        $roles = RoleTable::getInstance()->findAll();
        $forms = array();
        foreach ($roles as $role)
        {
            $forms[] = new RoleForm($role);
        }

        //for view
        $this->forms = $forms;
    }

    public function executeAddNewRole(sfWebRequest $request)
    {
        $form = new RoleForm();
        if ($request->isMethod('POST'))
        {
            $roleInfo  = $request->getParameter('role');
            $form->bind($roleInfo);
            $validFlag = '';
            if ($form->isValid())
            {
                $validFlag = true;
                /*
                 * save and return $role object
                 */
                $form->save();
                $return    = array('html'      => $this->getComponent('role', 'showRoles'), 'validFlag' => $validFlag);
            }
            else
            {
                $validFlag = false;
                $return    = array('html' => $this->getPartial('roleForm', array('form'      => $form)), 'validFlag' => $validFlag);
            }
        }
        else
        {
            $return = array('html' => $this->getPartial('roleForm', array('form' => $form)));
        }
        return $this->renderText(json_encode($return));
    }

    public function executeEditRole(sfWebRequest $request)
    {
        $roleId = $request->getParameter('id');
        $role   = RoleTable::getInstance()->findOneById($roleId);

        $form = new RoleForm($role);
        if ($request->isMethod('POST'))
        {
            $roleInfo  = $request->getParameter('role');
            $form->bind($roleInfo);
            $validFlag = '';
            if ($form->isValid())
            {
                $validFlag = true;
                /*
                 * save and return $role object
                 */
                $form->save();
                $return    = array('html'      => $this->getComponent('role', 'showRoles'), 'validFlag' => $validFlag);
            }
            else
            {
                $validFlag = false;
                $return    = array('html'      => $this->getComponent('role', 'showRoles'), 'validFlag' => $validFlag);
            }
        }
        else
        {
            $return = array('html' => $this->getPartial('roleForm', array('form' => $form)));
        }
        return $this->renderText(json_encode($return));
    }

    public function executeDeleteRole(sfWebRequest $request)
    {
        $roleId = $request->getParameter('id');
        $role   = new Role;
        $role   = RoleTable::getInstance()->findOneById($roleId);
        $this->forward404Unless($role);
        $i18    = sfContext::getInstance()->getI18N();
        /*
         * check if this role have users
         */
        if ($role->getUsers()->count() > 0)
        {
            /*
             * the role has users so we can't delete it
             */
            $return = array('msg'        => $i18->__('the role') . ' ' . $role->getTitle() . ' ' . 'have many users to delete it delete its users first', 'deleteFlag' => false);
        }
        else
        {
            $role->delete();
            $return = array(
                'msg'        => $i18->__('the role') . ' ' . $role->getTitle() . ' ' . 'deleted',
                'html'       => $this->getComponent('role', 'showRoles'),
                'deleteFlag' => true);
        }
        return $this->renderText(json_encode($return));
    }

}
